<?php

namespace common\utils;


/**
 *
 */
class TimeUtil
{

    public static function diffBetweenTwoDate($dateOne, $dateTwo)
    {
        $strtoDateOne = strtotime($dateOne);
        $strtoDateTwo = strtotime($dateTwo);

        if ($strtoDateOne < $strtoDateTwo) {
            $tmp = $strtoDateTwo;
            $strtoDateTwo = $strtoDateOne;
            $strtoDateOne = $tmp;
        }
        $diff = round(($strtoDateOne - $strtoDateTwo) / (60 * 60), 2);

        return $diff;
    }

    public static function diffDate($date1, $date2)
    {
        if (strtotime($date1) > strtotime($date2)) {
            $tmp = $date2;
            $date2 = $date1;
            $date1 = $tmp;
        }
        $date2 = date('Y-m-d', strtotime(" +1 day", strtotime($date2)));
        list($Y1, $m1, $d1) = explode('-', $date1);
        list($Y2, $m2, $d2) = explode('-', $date2);
        $Y = $Y2 - $Y1;
        $m = $m2 - $m1;
        $d = $d2 - $d1;
        if ($d < 0) {
            $d += (int)date('t', strtotime("-1 month $date2"));
            $m--;
        }
        if ($m < 0) {
            $m += 12;
            $Y--;
        }
        return array('year' => $Y, 'month' => $m, 'day' => $d);
    }

    public static function week($date)
    {
        $time = date("w", strtotime($date));

        $array = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];

        return $array[$time];
    }
}